<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>04-定时器的应用(三)</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #box1 {
            width: 100px;
            height: 100px;
            background-color: #bfa;
            position: absolute;
            left: 0;
        }

        #box2 {
            width: 100px;
            height: 100px;
            background-color: skyblue;
            position: absolute;
            left: 0;
            top: 200px;
        }
    </style>


    <script>

        window.onload = function () {

            var box1 = document.getElementById("box1");
            var box2 = document.getElementById("box2");

            var btn01 = document.getElementById("btn01");
            btn01.onclick = function () {
                move(box1, "left", 800, 20)
            }

            var btn02 = document.getElementById("btn02");
            btn02.onclick = function () {
                move(box1, "left", 0, 20)
            }

            var btn03 = document.getElementById("btn03");
            btn03.onclick = function () {
                move(box2, "left", 800, 10)
            }

            var btn04 = document.getElementById("btn04");
            btn04.onclick = function () {
                // move(box2, "width", 800, 10)
                // move(box2, "top", 800, 10)
                // move(box2, "height", 800, 10)
                move(box2, "width", 800, 10, function () {
                    move(box2, "height", 800, 10, function () {
                        move(box2, "top", 800, 10, function () {
                            move(box2, "width", 100, 10, function () {
                                move(box2, "height", 800, 10)
                            })
                        })
                    })
                })



            }

        }

        // var timer;

        function move(obj, attr, target, speed, callback) {

            clearInterval(obj.timer);

            var current = parseInt(getStyle(obj, attr));

            if (current > target) {
                speed = -speed
            }

            obj.timer = setInterval(function () {

                var oldValue = parseInt(getStyle(obj, attr));

                var newValue = oldValue + speed;

                if ((speed < 0 && newValue < target) || (speed > 0 && newValue > target)) {
                    newValue = target;
                }

                obj.style[attr] = newValue + "px";

                if (newValue == target) {
                    clearInterval(obj.timer);

                    callback && callback();
                }

            }, 30);

        }


        function getStyle(obj, name) {
            if (window.getComputedStyle) {
                return getComputedStyle(obj, null)[name];
            } else {
                return obj.currentStyle[name];
            }
        }


    </script>
</head>

<body>

    <button id="btn01">点击按钮以后box1向右移动</button>
    <button id="btn02">点击按钮以后box1向左移动</button>
    <button id="btn03">点击按钮以后box2向右移动</button>
    <button id="btn04">测试按钮</button>


    <br /><br />

    <div id="box1"></div>
    <div id="box2"></div>

    <div style="width: 0; height: 1000px; border-left: 1px solid black; position: absolute; left: 800px; top: 0;"></div>

</body>

</html>